package com.game.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.game.common.constant.Constant;
import com.game.entity.WebConfig;
import com.game.service.RedisService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.game.dao.WebConfigMapper;
import com.game.service.WebConfigService;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("webconfigService")
public class WebConfigServiceImpl extends ServiceImpl<WebConfigMapper, WebConfig> implements WebConfigService {

    @Resource
    private RedisService redisService;
    @Resource
    private WebConfigMapper webConfigMapper;

    /**
     * @return java.util.List<com.game.entity.Config>
     * @author 不愿透露
     * @date 启动开始 15:15
     */
    @PostConstruct
    @Override
    public void init() {
        List<WebConfig> list = this.list();
        JSONObject json = new JSONObject();
        for (WebConfig config : list) {
            Constant.configs.put(config.getName(), config.getValue());
            json.put(config.getName(), config.getValue());
        }
        redisService.set("webConfigs", json.toJSONString());
    }

    @Override
    public boolean updateByBody(JSONObject json) {
        Map<String, WebConfig> webConfigsMap = list().stream().collect(Collectors.toMap(WebConfig::getName, it -> it));
        List<WebConfig> update = new ArrayList<>();
        for (String key : webConfigsMap.keySet()) {
            WebConfig webConfig = webConfigsMap.get(key);
            if (json.getString(key) != null) {
                System.out.println(json.getString(key));
                webConfig.setValue(json.getString(key));
            }
            update.add(webConfig);
        }
        return updateBatchById(update);
    }

    @Override
    public WebConfig findByName(String name) {
        QueryWrapper<WebConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return webConfigMapper.selectOne(queryWrapper);
    }

    @Override
    public Map<String, String> findAll() {
        return this.list().stream().collect(
                Collectors.toMap(WebConfig::getName, it -> (StringUtils.isBlank(it.getValue()) ? "" : it.getValue())));
    }

    @Override
    public Map<String, String> findAllByEmail() {
        QueryWrapper<WebConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("name", new ArrayList<String>() {{
            add("MAIL_HOST");
            add("MAIL_ENCRYPTION");
            add("MAIL_PORT");
            add("MAIL_USERNAME");
            add("MAIL_PASSWORD");
        }});
        return webConfigMapper.selectList(queryWrapper).stream().collect(
                Collectors.toMap(WebConfig::getName, it -> (StringUtils.isBlank(it.getValue()) ? "" : it.getValue())));
    }
}
